#!/bin/bash
set -e

usage() {
cat << EOF
Usage: $0 [LABEL]
Push a newly-built image with the given LABEL to gcr.io and DockerHub.
See CHANGELOG.md file for LABEL naming convention.
EOF
}

set -x

SOURCE_IMAGE='kaggle/python-tensorflow-whl'
TARGET_IMAGE='gcr.io/kaggle-images/python-tensorflow-whl'

while :; do
    case "$1" in 
        -h|--help)
            usage
            exit
            ;;
        -?*)
            usage
            printf 'ERROR: Unknown option: %s\n' "$1" >&2
            exit
            ;;
        *)            
            break
    esac

    shift
done

LABEL=$1

if [[ -z "$LABEL" ]]; then
    echo 'You must provide a label for the image'
    exit 1
fi

readonly SOURCE_IMAGE
readonly TARGET_IMAGE
readonly LABEL

set -x
docker tag "$SOURCE_IMAGE:latest" "$TARGET_IMAGE:$LABEL"
gcloud docker -- push "$TARGET_IMAGE:$LABEL"
